package com.qf.web.controller;/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/20
 */

import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * projectName:Project_2202
 * author:
 * time:2022/4/20 7:45
 * description:
 */
@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet {
    CartService cartService = new CartServiceImpl();

   public String  addCart(HttpServletRequest request, HttpServletResponse response)throws IOException{
       Object userObj = request.getSession().getAttribute("user");
       if(userObj == null){
           return "redirect:/login.jsp";
       }
       String goodsId = request.getParameter("goodsId");
       String number = request.getParameter("number");
       if(StringUtils.isEmpty(goodsId)||StringUtils.isEmpty(number)){
           request.setAttribute("msg", "添加到购物车失败！！");
           return "forward:/message.jsp";
       }
       User user = (User)userObj;
       GoodsService goodsService = new GoodsServiceImpl();
       Goods goods = goodsService.findById(Integer.parseInt(goodsId));
       Cart cart = cartService.findByUidAndGid(user.getId(),Integer.parseInt(goodsId));

       if(cart==null){
           cart = new Cart();
           cart.setId(user.getId());
           cart.setPid(Integer.parseInt(goodsId));
           cart.setMoney(Integer.parseInt(number)*goods.getPrice());
           cart.setNum(Integer.parseInt(number));
           cartService.insert(cart);
       }else{
           cart.setNum(cart.getNum()+Integer.parseInt(number));
           cart.setMoney(goods.getPrice()*cart.getNum());
           cartService.updateCart(cart);
       }

        return "redirect:/cartSuccess.jsp";
   }

    public String  getCart(HttpServletRequest request, HttpServletResponse response)throws IOException{
        Object userObj = request.getSession().getAttribute("user");
        if(userObj == null){
            return "redirect:/login.jsp";
        }
        User user = (User)userObj;
        List<Cart> carts = cartService.findCardByUid(user.getId());
        request.setAttribute("carts", carts);
        return "forward:/cart.jsp";

    }
    public void addCartAjax(HttpServletRequest request, HttpServletResponse response)throws IOException{
        User user = (User)request.getSession().getAttribute("user");
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.findById(Integer.parseInt(goodsId));
        Cart cart = cartService.findByUidAndGid(user.getId(),Integer.parseInt(goodsId));
        if(cart == null){
            cart = new Cart();
            cart.setId(user.getId());
            cart.setPid(Integer.parseInt(goodsId));
            cart.setMoney(Integer.parseInt(number)*goods.getPrice());
            cart.setNum(Integer.parseInt(number));
            cartService.insert(cart);
        }else{
            if(Integer.parseInt(number)==0){
                cartService.deleteCart(user.getId(),Integer.parseInt(goodsId));
            }else{
                cart.setNum(cart.getNum()+Integer.parseInt(number));
                cart.setMoney(cart.getNum()*goods.getPrice());
                cartService.updateCart(cart);
            }
        }
        response.getWriter().print("success");

    }
    public void clearCartAjax(HttpServletRequest request, HttpServletResponse response)throws IOException{
        User user = (User)request.getSession().getAttribute("user");
        cartService.clearCart(user.getId());
        response.getWriter().print("success");
    }
}